public class Solution1248 {
    public int numberOfSubarrays(int[] nums, int k) {
        int count=0;
        int start=0,end=0;
        int d=-1;
        for (int i=0;i<nums.length;i++){
            if (nums[i]%2==1) {
                count++;
            }
            if (count==k){
                while (nums[end]%2==0){
                    end++;
                }
                d=i;
                break;
            }
        }
        if (count<k){
            return 0;
        }
        int ans=end-start+1;
        for (int i=d+1;i<nums.length;i++){
            if (nums[i] % 2 != 0) {
                start = end = end + 1;
                while (nums[end] % 2 == 0) {
                    end++;
                }
            }
            ans+=end-start+1;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1248().numberOfSubarrays(new int[]{1,1,2,1,1},3));
    }
}
